# -*- coding: utf-8 -*-#
#-------------------------------------------------------------------------------
# 建立者:        黄周云  
# Name:         daima_qidain
# Description:
# Author:       ASUS
# Date:         2019/4/30
#-------------------------------------------------------------------------------


import requests
from pyquery import PyQuery as pq
import os

class Qidian(object):
    def get_first_yemian(self):
        response1 = requests.get("https://www.qidian.com/all")
        doc1 = pq(response1.text)
        xsName_list = doc1(".book-mid-info h4 a").text().split()
        xsurl_list= []
        xsurl_biaoqian = doc1(".book-mid-info h4 a")
        for item in xsurl_biaoqian.items():
                xsUrl = item.attr('href')
                xsurl_list.append(xsUrl)
        for xsName,xsurl in zip(xsName_list,xsurl_list):
            if os.path.exists(xsName) == False:
                os.mkdir(xsName)
            self.get_second_yemian(xsName,xsurl)

    def get_second_yemian(self,xsName,xsurl):
        response2 = requests.get('http:'+ xsurl)
        doc2 = pq(response2.text)
        chptName_list = doc2(".volume-wrap .cf li a").text().split()
        chpturl_list = []
        chpturl_biaoqian = doc2(".volume-wrap .cf li a")
        for item in chpturl_biaoqian.items():
            chptUrl = item.attr('href')
            chpturl_list.append(chptUrl)
        for chptName,chpturl in zip(chptName_list,chpturl_list):
            self.get_three_yemian(xsName,chptName,chpturl)

    def get_three_yemian(self,xsName,chptName,chpturl):
        response3 = requests.get('http:'+ chpturl)
        doc3 = pq(response3.text)
        zhengwen = doc3(".read-content.j_readContent p").text().split()
        filename = xsName+"/"+chptName+".txt"
        print("正在爬取",filename)
        with open(filename,"w",encoding='utf-8') as f:
            for item in zhengwen:
                f.write(item)
                f.write("\n")



if __name__ == '__main__':
    qidian = Qidian()
    qidian.get_first_yemian()








